package com.cfpl.admin_service.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_service.mapper.AdminMapper;
import com.cfpl.admin_service.mapper.RoleMapper;
import com.cfpl.admin_service.service.RoleService;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.Admin;
import com.cfpl.project_common.pojo.Permission;
import com.cfpl.project_common.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 14720
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2023-10-10 11:09:53
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminMapper adminMapper;

    /**
     * 添加角色信息
     * @param role 角色对象
     * 角色信息和角色权限中间表信息
     */
    @Override
    @Transactional
    public void insertRoleInfo(Role role) {
        if (StrUtil.isBlank(role.getRoleName()) || StrUtil.isBlank(role.getRoleDescription())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        System.out.println(role);
        //插入角色信息
        roleMapper.insert(role);

        //获取所有权限ID集合
        if (role.getPermissionList() != null){
            List<Permission> rolePermissionList = role.getPermissionList();
            Long[] permissionIds = new Long[rolePermissionList.size()];
            for (int i = 0; i < rolePermissionList.size(); i++) {
                permissionIds[i] = rolePermissionList.get(i).getPermissionId();
            }
            roleMapper.assignPermission(role.getRoleId(), permissionIds);
        }

    }

    /**
     * 更新角色信息
     * @param role 角色对象
     */
    @Override
    @Transactional
    public void updateRoleInfo(Role role) {
        if (StrUtil.isBlank(role.getRoleName()) || StrUtil.isBlank(role.getRoleDescription())){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        roleMapper.updateById(role);
    }

    /**
     * 给角色分配权限
     */
    @Override
    @Transactional
    public void assignPermission(Long roleId, Long[] permissionIds) {
        if (roleId == null){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        if (permissionIds == null || permissionIds.length < 1){
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        roleMapper.deleteRole_Permission(roleId);
        roleMapper.assignPermission(roleId, permissionIds);
    }

    /**
     * 删除角色权限列表
     * @param roleId 角色编号
     */
    @Override
    @Transactional
    public void deleteRole_Permission(Long roleId) {
        roleMapper.deleteById(roleId);
        roleMapper.deleteRole_Permission(roleId);
    }

    /**
     * 查询角色以及所属权限
     * @param roleId 角色编号
     * @return role
     */
    @Override
    public Role queryRoleInfoById(Long roleId) {
       return roleMapper.queryRoleInfoById(roleId);
    }

    /**
     * 查询角色信息是否正在被使用
     * @param roleIds 角色编号
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRoleByIds(List<Long> roleIds) {
        List<Admin> adminList = adminMapper.queryRoleIsUsing(roleIds);
        //查出结果说明角色信息正在使用
        if (!adminList.isEmpty()  ){
            throw new BusException(CodeEnum.ROLE_IS_USING);
        }
        //删除角色权限信息中间表
        roleMapper.deleteRole_PermissionByIds(roleIds);
        //删除角色表
        roleMapper.deleteBatchIds(roleIds);
    }


}




